<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <base th:href="${#httpServletRequest.getContextPath()}">
    <title>修改院系</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="../../static/layui/css/layui.css" th:href="@{/layui/css/layui.css}">
    <!-- 引入 layui.js -->
    <script src="../../static/layui/layui.js" th:src="@{/layui/layui.js}"></script>
</head>
<body style="width: 99%">
<div style="width: 630px">
    <div>
        <div class="layui-card">
            <div class="layui-card-body layui-bg-gray">
                <form class="layui-form" lay-filter="addUserForm">
                    <div class=" layui-form-item" style="display: none">
                        <label class="layui-form-label">院系代号</label>
                        <div class="layui-input-block">
                            <label>
                                <input type="text" name="id" th:value="${department.id}" class="layui-input">
                            </label>
                        </div>
                    </div>
                    <div class=" layui-form-item">
                        <label class="layui-form-label">院系名称</label>
                        <div class="layui-input-block">
                            <label>
                                <input type="text" name="name" lay-verify="name" autocomplete="off"
                                       placeholder="请输入姓名" th:value="${department.name}" class="layui-input">
                            </label>
                        </div>
                    </div>

                    <div class="layui-btn-container" style="margin-left: 435px">
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        <button type="submit" class="layui-btn" style="margin-right: 0" lay-submit=""
                                lay-filter="modifySubmit">修改
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        const form = layui.form
            , layer = layui.layer
            , $ = layui.$;


        //自定义验证规则
        form.verify({
            name: [
                /^[\u4e00-\u9fa5_a-zA-Z0-9]{2,5}$/,
                '姓名必须为2-5个字符'
            ]
            , notNull: [
                /^[\s\S]*.*[^\s][\s\S]*$/,
                '此项不能为空'
            ]
            , phone: [
                /^1[3456789][0-9]{9}$/
                , '手机号码格式错误'
            ]
        });

        // 监听提交
        form.on('submit(modifySubmit)', function (data) {
            $.ajax({
                url: "/department/update",
                type: "post",
                data: JSON.stringify(data.field),
                contentType: "application/json;charset=utf8",
                dataType: "json",
                success: function (response) {
                    let code = response.code;
                    if (code == 0) {
                        layer.msg("修改成功!", {icon: 1, offset: "auto", time: 3000});
                        setTimeout(function () {
                            let index = parent.layer.getFrameIndex(window.name); // 获取当前窗口的name
                            parent.layer.close(index);
                        }, 3000);
                    } else {
                        layer.msg("修改失败! " + response.code + " " + response.msg, {
                            icon: 0,
                            offset: "auto",
                            time: 2000
                        });
                    }
                },
                error: function (response) {
                    layer.msg("修改失败! " + response.status + " " + response.statusText, {
                        icon: 0,
                        offset: "auto",
                        time: 2000
                    });
                }
            })
            return false;
        });

        layui.use('laydate', function () {
            let laydate = layui.laydate;
            laydate.render({
                elem: '#birthday'
            });
        });
    });
</script>
</html>